<?php
/**
 * Object Relational Persistence Mapping Class for table: lab_assigned_item
 *
 * @package modules.dept_laboratory.ordo
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class LabAssignedItem extends ORDataObject {

	/**#@+
	 * Fields of table: lab_assigned_item mapped to class members
	 */
	var $lab_assigned_item_id		= '';
	var $lab_ticket_id		= '';
	var $service_id		= '';
	var $original_price		= '';
	var $insurance_price		= '';
	var $discount		= '';
	var $patient_price		= '';
	var $is_done		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'lab_assigned_item';

	/**
	 * Primary Key
	 */
	var $_key = 'lab_assigned_item_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'LabAssignedItem';
	var $nameText = '';

	/**
	 * Handle instantiation
	 */
	function __construct() {
		parent::ORDataObject();
	}
	
    public function setData($labAssignedItemInfo) {
       if (is_array($labAssignedItemInfo)) {
            $this->populateArray($labAssignedItemInfo);
        }
    }
    
    /**
     * Delete all items of a givent labTicketId.
     * @param $labTicketId
     * @return labTicketId
     */
    public function deleleAssignedItems($labTicketId) {
        $sql = "DELETE FROM $this->_table WHERE lab_ticket_id='$labTicketId'";
        $this->_execute($sql);
        return $labTicketId;      
    }
    /**
     * List all item of given $labTicketId
     * @param $labTicketId
     * @return array[n]()
     */    
    public function listLabAssignedItems($labTicketId, $short = false) {
    	$field    =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
        $ds =& new Datasource_sql();
        if ($short == false) {
	        $sql = array(
	            'cols'  => "lab_assigned_item_id, t.service_id, s.$field name ,
	                        t.original_price, t.insurance_price, t.patient_price, is_done",
	            'from'  => "$this->_table t INNER JOIN service s ON(t.service_id=s.service_id) 
	                        ",              
	            'where' =>   "lab_ticket_id ='$labTicketId'"            
	            );
        } else {
        	$sql = array(
                'cols'  => "lab_assigned_item_id",
                'from'  => "$this->_table",              
                'where' =>   "lab_ticket_id ='$labTicketId'"            
                );
        	
        }
        $ds->setup($this->_db, $sql);
        return $ds->toArray();
    	
    }
    public function setup($id = 0) {
        parent::setup($id);
        $field    =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
                
        $ds =& new Datasource_sql();
        $sql = array(
                'cols'  => "s.$field name",
                'from'  => "$this->_table t 
                            INNER JOIN service s ON(t.service_id=s.service_id) 
                            ",              
                'where' =>   "lab_assigned_item_id ='$id'"            
        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (!empty($result)) {
        	$this->nameText = $result[0]['name'];
        }
        
    }
    public function getNameText() {
    	return $this->nameText;
    }
    public function getIsDone() {
        return $this->is_done;
    }
    public function getPatientPrice() {
        return $this->patient_price;
    }
    public function getInsurancePrice() {
        return $this->insurance_price;
    }
    public function getOriginalPrice() {
        return $this->original_price;
    }
    public function getLabTicketId() {
    	return $this->getLabTicketId;
    }
    public function getServiceId() {
        return $this->service_id;
    }
    
    public function setDone($done) {    	
    	$sql = "UPDATE $this->_table SET is_done='$done' WHERE $this->_key='$this->lab_assigned_item_id'";
    	$this->_execute($sql);
    	 $this->is_done = $done;
    }

	
}
?>
